<?php

namespace App\Repositories\Admin;

use App\BaseRepository;
use App\Helpers\Helpers;
use App\Model\ProductAttrLabel;
use App\Model\ProductAttrValue;
use App\Model\ProductSkuAttr;
use App\Model\WineProduct;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;

class ProductSkuRepository extends BaseRepository
{
    //模型名称
    protected $modelName = "App\Model\ProductSku";

    protected $appendCreatorAndUpdater = true;

    protected $appendUserId = true;

    protected $product;

    protected $skuAttr = [];

    protected function getShowRespondData($model)
    {
        //return $model->load('skuAttr');
        return $model->load('groupAttr','groupAttr.labels','groupAttr.labels.values','skuAttr');
    }

    protected function getUpdateRespondData($model)
    {
        return $model->load('groupAttr','groupAttr.labels','groupAttr.labels.values','skuAttr');
    }

    protected function commonRules()
    {
        return [
            'product_id' => [
                'required',
                Rule::exists('wine_product','id')->whereNull('deleted_at'),
            ],
            'origin_price' => 'required|min:0.01',
            'price' => 'required|min:0.01',
            'price_3_4' => 'required|min:0.01',
            'price_5' => 'required|min:0.01',
            'sku' => 'required|min:1',
            'sku_attr' => 'required|array'
        ];
    }
    protected function appendStoreFillData()
    {
        return [
            'skuCode' => Helpers::createSerial('product_skus','skuCode','S'),
        ];
    }
    protected function appendCommonFillData()
    {
        return [
            'attr_group_id' => $this->product->attr_group_id,
        ];
    }

    protected function commonMessages()
    {
        return [
            'product_id.exists' => '未选择产品，或产品未选择属性',
            'origin_price.required'=> '市场价格必填',
            'price.required'=> '1档位价格必填',
            'price_3_4.required'=> '3档位价格必填',
            'price_5.required'=>  '5档位价格必填',
            'sku.required'=> '库存必填',
            'attr_group_id.min'=> '市场价格需大于0',
            'price.min'=> '1档位价格需大于0',
            'price_3_4.min'=> '3档位价格需大于0',
            'price_5.min'=> '5档位价格需大于0',
            'sku.min'=> '库存需大于0',
        ];
    }

    //自定义公共验证
    protected function processCommonValidate()
    {
        $this->product = WineProduct::find($this->request->input('product_id'));

        $sku_attr = $this->request->input('sku_attr');

        foreach ($sku_attr as $sku){
            if (isset($sku['attr_label_id']) && isset($sku['attr_value_id'])){

//                $c = DB::table('product_sku_attrs')->where('product_id','=',$this->request->input('product_id'))
//                    ->where('attr_label_id','=',$sku['attr_label_id'])
//                    ->where('attr_value_id','=',$sku['attr_value_id']);
//
//                if(isset($sku['id'])) $c = $c->where('id','<>',$sku['id']);
//
//                if ($c->count()>0)  {
//                    $this->error = '已存在该属性SKU，请勿重复添加！';
//                    return false;
//                }

                $label = ProductAttrLabel::where('attr_group_id','=',$this->product ->attr_group_id)->find($sku['attr_label_id']);
                if (!$label) {
                    $this->error = '不存在该属性！';
                    return false;
                }

                $value = ProductAttrValue::where('attr_label_id','=',$label->id)->find($sku['attr_value_id']);
                if (!$value) {
                    $this->error = '不存在该属性值！';
                    return false;
                }

                $this->skuAttr[] = [$label,$value];

            }else{
                $this->error = '属性参数错误！';
                return false;
            }
        }

        if (count($this->skuAttr) == 0) {
            $this->error = '请填写属性参数！';
            return false;
        }

        return true;
    }

    protected function processUpdateValidate($id)
    {
        $c = DB::table('orders')->where('sku_id','=',$id)->whereNull('deleted_at')->count();
        if ($c>0){
            $this->error = '已存在该SKU的订单，不能再进行编辑属性';
            return false;
        }
        return true;
    }

    protected function commonSave($model)
    {
        $ids = [];

        $desc = [];

        foreach ($this->skuAttr as $sku) {

            $sk = ProductSkuAttr::where('attr_label_id','=',$sku[0]->id)
                ->where('attr_value_id','=',$sku[1]->id)
                ->where('product_sku_id','=',$model->id)
                ->first();

            $sk = $sk?$sk:new ProductSkuAttr();
            $sk->product_id = $model->product_id;
            $sk->product_sku_id = $model->id;
            $sk->attr_label_id = $sku[0]->id;
            $sk->attr_value_id = $sku[1]->id;
            $sk->attrLabelName = $sku[0]->name;
            $sk->attrLabelValue = $sku[1]->value;
            $sk->save();

            $desc[] = $sk->attrLabelName .":".$sk->attrLabelValue;

            $ids[] = $sk->id;
        }

        DB::table('product_sku_attrs')->where('product_sku_id','=',$model->id)->whereNotIn('id',$ids)->delete();

        WineProduct::updateSkuCount($model->product_id);

        $model->desc = implode(',',$desc);
        $model->consume_price = 0.9*$model->price;
        $model->save();

        return true;
    }

    protected function destroyValidate($id)
    {
        $c = DB::table('orders')->where('sku_id','=',$id)->whereNull('deleted_at')->count();
        if ($c>0){
            $this->error = '已存在该SKU的订单，不能删除';
            return false;
        }
        return true;
    }
}